const initWebsocket = require("../../config/websocket.config");
/**
 * 通用接口，由操作集触发的接口和通过操作集调用3d场景的事件都可以通过该接口实现
 * @param {Obeject} data 传递的数据.
 * @param {string} toguys 传向何处(thingjs:3d场景端，web:大屏端).
 */

function getClientIp(req) {
  return (
    
    req.headers['x-forwarded-for'] || // 判断是否有反向代理 IP
    req.headers['x-real-ip'] || // 判断 connection 的远程 IP
    req.socket.remoteAddress.split('::ffff:').join('')
  );
}

let positionArr = {
  '模拟停车1': [
    [-23.78165626, 0.2605447, 119.50931818],
    [-23.66499227, 0.10622274, 115.95100532],
    [-23.58525217, 0.10622289, 111.58182],
    [-21.4825421, 0.10996324, 109.17701387],
    [-18.58031841, 0.10996325, 108.96307909],
    [-15.50301225, 0.10996325, 108.96500455],
    [-12.72307301, 0.10996325, 109.04008237],
    [-11.47728259, 0.10996331, 107.0766612],
    [-11.29730244, 0.10996345, 103.20193305],
    [-11.64878926, 0.10996358, 99.26413559],
    [-13.30155984, 0.10996366, 96.96510139],
    [-16.18501685, 0.10996366, 96.98168953],
  ],
  '模拟停车2': [
    [-23.78165626, 0.2605447, 119.50931818],
    [-23.66499227, 0.10622274, 115.95100532],
    [-23.58525217, 0.10622289, 111.58182],
    [-21.4825421, 0.10996324, 109.17701387],
    [-18.58031841, 0.10996325, 108.96307909],
    [-15.50301225, 0.10996325, 108.96500455],
    [-12.72307301, 0.10996325, 109.04008237],
    [-11.47728259, 0.10996331, 107.0766612],
    [-11.29730244, 0.10996345, 103.20193305],
    [-11.57521043, 0.10996358, 99.1976532],
    [-12.18272479, 0.10996373, 94.84075787],
    [-16.24837931, 0.10996378, 93.56935846],
  ],
  '模拟停车3': [
    [-23.79229595, 0.56954692, 119.57680458],
    [-23.78141456, 0.10622272, 116.41690963],
    [-23.47765797, 0.10996319, 110.80874495],
    [-19.70621377, 0.1099632, 110.28592783],
    [-15.81340997, 0.10996321, 110.16287127],
    [-12.05159763, 0.10996324, 109.29650172],
    [-11.65832697, 0.10996339, 104.72528134],
    [-11.59829316, 0.10996361, 98.32253342],
    [-11.61999028, 0.10996377, 93.77186546],
    [-11.68592554, 0.10996388, 90.58498813],
    [-16.30406537, 0.10996388, 90.45411598],
  ],
  '模拟停车4': [
    [-23.75855655, 0.26614435, 119.55113017],
    [-23.94286982, 0.10622279, 114.25698504],
    [-23.84516465, 0.10996318, 111.00404036],
    [-26.18166592, 0.10996322, 109.72452142],
    [-30.46227517, 0.10996323, 109.53367222],
    [-35.94665757, 0.10996324, 109.11730525],
    [-41.41464323, 0.10996323, 109.48713423],
    [-48.63350731, 0.10996319, 110.67095145],
  ],
  '模拟停车5': [
    [-23.75855655, 0.26614435, 119.55113017],
    [-23.94286982, 0.10622279, 114.25698504],
    [-23.84516465, 0.10996318, 111.00404036],
    [-26.18166592, 0.10996322, 109.72452142],
    [-30.46227517, 0.10996323, 109.53367222],
    [-35.94665757, 0.10996324, 109.11730525],
    [-41.41464323, 0.10996323, 109.48713423],
    [-44.15042995, 0.10996325, 108.92039511],
    [-48.45783884, 0.10996331, 107.22345474],
  ],
  '模拟停车6': [
    [-23.75855655, 0.26614435, 119.55113017],
    [-23.94286982, 0.10622279, 114.25698504],
    [-23.84516465, 0.10996318, 111.00404036],
    [-26.18166592, 0.10996322, 109.72452142],
    [-30.46227517, 0.10996323, 109.53367222],
    [-35.94665757, 0.10996324, 109.11730525],
    [-41.41464323, 0.10996323, 109.48713423],
    [-44.15042995, 0.10996325, 108.92039511],
    [-44.12491087, 0.10996336, 105.77810811],
    [-45.37849272, 0.10996343, 103.7198933],
    [-48.53578249, 0.10996343, 103.72663783],
  ],
  '模拟停车7': [
    [-23.75855655, 0.26614435, 119.55113017],
    [-23.94286982, 0.10622279, 114.25698504],
    [-23.84516465, 0.10996318, 111.00404036],
    [-26.18166592, 0.10996322, 109.72452142],
    [-30.46227517, 0.10996323, 109.53367222],
    [-35.94665757, 0.10996324, 109.11730525],
    [-41.41464323, 0.10996323, 109.48713423],
    [-44.15042995, 0.10996325, 108.92039511],
    [-44.12491087, 0.10996336, 105.77810811],
    [-44.2028776, 0.10996351, 101.41331767],
    [-44.373952, 0.10996368, 96.26822221],
    [-44.27003194, 0.10996388, 90.5157234],
    [-45.13579803, 0.10996399, 87.34724679],
    [-48.48280119, 0.10996399, 87.23880565],
  ],
  '模拟停车8': [
    [-23.75855655, 0.26614435, 119.55113017],
    [-23.94286982, 0.10622279, 114.25698504],
    [-23.84516465, 0.10996318, 111.00404036],
    [-26.18166592, 0.10996322, 109.72452142],
    [-30.46227517, 0.10996323, 109.53367222],
    [-35.94665757, 0.10996324, 109.11730525],
    [-41.41464323, 0.10996323, 109.48713423],
    [-44.15042995, 0.10996325, 108.92039511],
    [-44.12491087, 0.10996336, 105.77810811],
    [-44.2028776, 0.10996351, 101.41331767],
    [-44.22781907, 0.10996379, 93.25999664],
    [-44.13098262, 0.10996407, 84.87825903],
    [-48.71219394, 0.10996414, 82.87281114],
  ],
  '模拟停车9': [
    [-23.75855655, 0.26614435, 119.55113017],
    [-23.94286982, 0.10622279, 114.25698504],
    [-23.84516465, 0.10996318, 111.00404036],
    [-26.18166592, 0.10996322, 109.72452142],
    [-30.46227517, 0.10996323, 109.53367222],
    [-35.94665757, 0.10996324, 109.11730525],
    [-41.41464323, 0.10996323, 109.48713423],
    [-44.15042995, 0.10996325, 108.92039511],
    [-44.12491087, 0.10996336, 105.77810811],
    [-44.2028776, 0.10996351, 101.41331767],
    [-44.22781907, 0.10996379, 93.25999664],
    [-43.72031089, 0.10996397, 87.90975421],
    [-43.84453887, 0.10996423, 80.34245923],
    [-48.54619254, 0.10996427, 79.06039908],
  ],
};

let texture = {
  '模拟停车1': "停车路径1",
  '模拟停车2': "停车路径2",
  '模拟停车3': "停车路径3",
  '模拟停车4': "停车路径4",
  '模拟停车5': "停车路径5",
  '模拟停车6': "停车路径6",
  '模拟停车7': "停车路径7",
  '模拟停车8': "停车路径8",
  '模拟停车9': "停车路径9",
};


const commonController = {
  commonApi: (req, res) => {
    try {
      console.log(getClientIp(req));
      let ip = getClientIp(req);
      const { data, toguys, type, eventName } = req.body;
      if (!toguys) {
        res.send({
          code: 502,
          message: "请确定要通信的用户",
        });
        return;
      }
      let info = {
        eventName: eventName,
        toguys: toguys,
        data,
        type: type,
      };

      let result = initWebsocket.callPhone(info, ip);
      if (result) {
        res.send({
          code: 200,
          message: "success",
        });
      } else {
        res.send({
          code: 500,
          message: "客户端未连接",
        });
      }
    } catch (error) {
      res.send({
        code: 501,
        message: error,
      });
    }
  },
  findItAsset: (req, res) => {
    try {
      console.log(getClientIp(req));
      let ip = getClientIp(req);
      const { data, toguys, type } = req.body;
      if (!toguys) {
        res.send({
          code: 502,
          message: "请确定要通信的用户",
        });
        return;
      }
      if (data.id == "" || data.id == null) {
        res.send({
          code: 500,
          message: "找不到此设备",
        });
        return;
      }
      let info = {
        toguys: toguys,
        data: {
          param: data,
          type: type,
        },
      };
      let result = initWebsocket.callPhone(info, ip);
      if (result) {
        res.send({
          code: 200,
          message: "success",
        });
      } else {
        res.send({
          code: 500,
          message: "客户端未连接",
        });
      }
    } catch (error) {
      res.send({
        code: 501,
        message: error,
      });
    }
  },
  sendBack: (req, res) => {
    try {
      console.log(getClientIp(req));
      let ip = getClientIp(req);
      const { data, toguys, type } = req.body.data;
      if (!toguys) {
        res.send({
          code: 502,
          message: "请确定要通信的用户",
        });
        return;
      }
      let info = {
        toguys: toguys,
        data: { data: data, type: type },
        type: type,
      };

      let result = initWebsocket.callPhone(info, ip);
      if (result) {
        res.send({
          code: 200,
          message: "success",
        });
      } else {
        res.send({
          code: 500,
          message: "客户端未连接",
        });
      }
    } catch (error) {
      res.send({
        code: 501,
        message: error,
      });
    }
  },
  // *模拟
  moni: (req, res) => {
    try {
      console.log(getClientIp(req));
      let ip = getClientIp(req);
      const { toguys, eventName, flag ,data} = req.body;

      if (!toguys) {
        res.send({
          code: 502,
          message: "请确定要通信的用户",
        });
        return;
      }
      let info = {
        eventName: eventName,
        toguys: toguys,
        data
      };

      let result = initWebsocket.callPhone(info, ip);

      if (result) {
        res.send({
          code: 200,
          message: "success",
        });
      } else {
        res.send({
          code: 500,
          message: "客户端未连接",
        });
      }
    } catch (error) {
      res.send({
        code: 501,
        message: error,
      });
    }
  },

  // *模拟停车
  moniStopCar: (req, res) => {
    try {
      console.log(getClientIp(req));
      let ip = getClientIp(req);
      const { toguys, eventName, flag } = req.body;
      if (!toguys) {
        res.send({
          code: 502,
          message: "请确定要通信的用户",
        });
        return;
      }

      let info = {
        eventName: eventName,
        toguys: toguys,
      };

      let result = initWebsocket.callPhone(info, ip);

      if (result) {
        res.send({
          code: 200,
          message: "success",
        });
      } else {
        res.send({
          code: 500,
          message: "客户端未连接",
        });
      }
    } catch (error) {
      res.send({
        code: 501,
        message: error,
      });
    }
  },
  // *返回模拟数据
  backMoni: (req, res) => {
    try {
      console.log(getClientIp(req));
      let ip = getClientIp(req);
      const { data, type, toguys } = req.body.data;

      if (data && type == "路径") {
        let info = {
          eventName: "模拟停车路线",
          data: {
            id: data,
            rid: texture[data],
            position: positionArr[data],
          },
          toguys: toguys,
        };

        let result = initWebsocket.callPhone(info, ip);
        if (result) {
          res.send({
            code: 200,
            message: "success",
          });
        } else {
          res.send({
            code: 500,
            message: "error",
          });
        }
      } else {
        res.send({
          code: 500,
          message: "未选择停车位",
        });
      }
    } catch {
      res.send({
        code: 500,
        message: "error",
      });
    }
  },
};

module.exports = commonController;
